package cn.kgc.service.impl;

import cn.kgc.entity.Users;
import cn.kgc.excpetion.GraceException;
import cn.kgc.feign.ResumeFeignClient;
import cn.kgc.grace.result.GraceJSONResult;
import cn.kgc.grace.result.ResponseStatusEnum;
import cn.kgc.mapper.UsersMapper;
import cn.kgc.service.UsersService;
import cn.kgc.service.UsersTccService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * @author k
 */
@Slf4j
@Service("usersService")
@SuppressWarnings("all")
public class UsersServiceImpl extends ServiceImpl<UsersMapper, Users> implements UsersService {
    @Resource
    private ResumeFeignClient resumeFeignClient;
    @Resource
    private UsersTccService usersTccService;

    @Override
    public Users login(String mobile) {
        //根据手机号查询用户信息,如果用户不为空就直接返回用户信息
        LambdaQueryWrapper<Users> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(Users::getMobile,mobile);
        return this.getOne(queryWrapper);
    }

    /**
     * 注册用户并且初始化简历
     * @param mobile 手机号
     */
    @Transactional
    @GlobalTransactional
    public Users registerAndInitResume(String mobile){
        //注册用户
         Users users= usersTccService.registerUser(null,mobile);
        //初始化简历
        GraceJSONResult graceJSONResult=resumeFeignClient.initResume(users.getId());
        if (((Boolean) graceJSONResult.getData())==false){ //如果初始化简历失败
            GraceException.display(ResponseStatusEnum.RESUME_INIT_ERROR); //抛出异常
        }
        return users;
    }
}